
import { Obj, Reg, Comb, Obuf, Ibuf, M9k, Mout, Mul, } from './defs';

export const filter = (
  objs: Obj[],

  regs: Reg[],
  combs: Comb[],
  obufs: Obuf[],
  ibufs: Ibuf[],
  m9ks: M9k[],
  mouts: Mout[],
  muls: Mul[],
  outMap: { [key: string]: Obj },
) => {

  var types: any = {};
  objs.forEach((obj) => {
    obj.type = obj.lines[1].split(" ")[0];
    obj.name = obj.lines[1].split(" ")[1];
    if (obj.type == "dffeas") {
      var reg = obj as Reg;
      obj.lines.forEach((line) => {
        
        if (line.startsWith(".clk(")) {
          reg.clk = line.substring(2 + 3, line.length - 2).trim();
        }
        if (line.startsWith(".d(")) {
          reg.in = line.substring(2 + 1, line.length - 2).trim();
        }
        if (line.startsWith(".ena(")) {
          reg.ena = line.substring(2 + 3, line.length - 2).trim();
        }
        if (line.startsWith(".sload(")) {
          reg.sload = line.substring(2 + 5, line.length - 2).trim();
        }
        if (line.startsWith(".sclr(")) {
          reg.sclr = line.substring(2 + 4, line.length - 2).trim();
        }
        if (line.startsWith(".asdata(")) {
          reg.asdata = line.substring(2 + 6, line.length - 2).trim();
        }
        if (line.startsWith(".q(")) {
          reg.out = line.substring(2 + 1, line.length - 2).trim();
        }
      });
      if (!outMap[reg.out]) {
        outMap[reg.out] = reg;
      } else {
        console.log("fds");
      }
      regs.push(reg);
    }

    if (obj.type == "cycloneive_lcell_comb") {
      var comb = obj as Comb;
      obj.lines.forEach((line) => {
        if (line.startsWith(".dataa(")) {
          comb.in1 = line.substring(2 + 5, line.length - 2).trim();
        }
        if (line.startsWith(".datab(")) {
          comb.in2 = line.substring(2 + 5, line.length - 2).trim();
        }
        if (line.startsWith(".datac(")) {
          comb.in3 = line.substring(2 + 5, line.length - 2).trim();
        }
        if (line.startsWith(".datad(")) {
          comb.in4 = line.substring(2 + 5, line.length - 2).trim();
        }
        if (line.startsWith(".cin(")) {
          comb.inc = line.substring(2 + 3, line.length - 2).trim();
        }

        if (line.startsWith(".combout(")) {
          comb.out = line.substring(2 + 7, line.length - 2).trim();
        }
        if (line.startsWith(".cout(")) {
          comb.outc = line.substring(2 + 4, line.length - 3).trim();
        }

      });

      if (comb.out) {
        if (!outMap[comb.out]) {
          outMap[comb.out] = comb;
        } else {
          console.log("fds");
        }
      }

      if (comb.outc) {
        if (!outMap[comb.outc]) {
          outMap[comb.outc] = comb;
        } else {
          console.log("fds");
        }
      }
      combs.push(comb);
    }

    if (obj.type == "cycloneive_io_ibuf") {
      var ibuf = obj as Ibuf;
      obj.lines.forEach((line) => {
        if (line.startsWith(".o(")) {
          ibuf.out = line.substring(2 + 1, line.length - 3).trim();
        }
      });

      if (ibuf.out) {
        if (!outMap[ibuf.out]) {
          outMap[ibuf.out] = ibuf;
        } else {
          console.log("fds");
        }
      }

      ibufs.push(ibuf);
    }

    if (obj.type == "cycloneive_io_obuf") {
      var obuf = obj as Obuf;
      obj.lines.forEach((line) => {
        if (line.startsWith(".i(")) {
          obuf.in = line.substring(2 + 1, line.length - 2).trim();
        }
      });
      obj.lines.forEach((line) => {
        if (line.startsWith(".oe(")) {
          obuf.oe = line.substring(2 + 2, line.length - 2).trim();
        }
      });

      obufs.push(obuf);
    }

    if (obj.type == "cycloneive_ram_block") {
      var m9k = obj as M9k;
      obj.lines.forEach((line) => {
        if (line.startsWith(".portadataout(")) {
          m9k.outa = line.substring(2 + 12, line.length - 2).trim();
        }
        if (line.startsWith(".portbdataout(")) {
          m9k.outb = line.substring(2 + 12, line.length - 3).trim();
        }
      });

      if (m9k.outa) {
        if (!outMap[m9k.outa]) {
          outMap[m9k.outa] = m9k;
        } else {
          console.log("fds");
        }
      }

      if (m9k.outb) {
        if (!outMap[m9k.outb]) {
          outMap[m9k.outb] = m9k;
        } else {
          console.log("fds");
        }
      }

      m9ks.push(m9k);
    }

    if (obj.type == "cycloneive_mac_out") {
      var mul = obj as Mul;
      obj.lines.forEach((line) => {
        if (line.startsWith(".dataa(")) {
          mul.dataa = line.substring(2 + 5, line.length - 3).trim();
        }
        if (line.startsWith(".dataout(")) {
          mul.out = line.substring(2 + 7, line.length - 3).trim();
        }
      });

      if (mul.out) {
        if (!outMap[mul.out]) {
          outMap[mul.out] = mul;
        } else {
          console.log("fds");
        }
      }

      muls.push(mul);
    }

    if (obj.type == "cycloneive_mac_mult") {
      var mul = obj as Mul;
      obj.lines.forEach((line) => {
        if (line.startsWith(".dataa(")) {
          mul.dataa = line.substring(2 + 5, line.length - 3).trim();
        }
        if (line.startsWith(".datab(")) {
          mul.datab = line.substring(2 + 5, line.length - 3).trim();
        }
        if (line.startsWith(".dataout(")) {
          mul.out = line.substring(2 + 7, line.length - 3).trim();
        }
      });

      if (mul.out) {
        if (!outMap[mul.out]) {
          outMap[mul.out] = mul;
        } else {
          console.log("fds");
        }
      }

      muls.push(mul);
    }


    (obj as any).lines = null;
    // obj.lines.forEach((line) => {
    //   if (line) {
    //     outtext.push(line);
    //   }
    // });
    // outtext.push("");

    if (!types[obj.type]) {
      types[obj.type] = 0;
    }
    types[obj.type]++;
  });

  console.log(types);
};